package com.fortune.controller;

import com.faneui.common.bean.BaseResp;
import com.faneui.common.bean.IdReq;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.DateFormatUtils;
import com.fortune.bean.BillInfoVo;
import com.fortune.dal.model.BillPayRecord;
import com.fortune.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("billinfo")
public class BillInfoController extends AbstractController {

    @Autowired
    private BillInfoService billInfoService;

    @Autowired
    private DebtService debtService;

    @Autowired
    private CustService custService;

    @Autowired
    private CreditCardInfoService creditCardInfoService ;

    @Autowired
    private CardService cardService;

    @Autowired
    private BillPayRecordService billPayRecordService ;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView indexOfAll() throws BusinessException {
        ModelAndView mv = new ModelAndView("billinfo");
        mv.addObject("menuFlag", "billinfo");
        return mv;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<BillInfoVo> list(@ModelAttribute BillInfoVo billInfoVo) throws BusinessException {
//        BillInfo param = new BillInfo();
//        param.setStatus("R");
//        List<BillInfo> billInfos = billInfoService.queryDebtBillList(param);
//        List<BillInfoVo> billInfoVos = Lists.newArrayList();
//        Map<Integer, Cust> custCache = Maps.newHashMap();
//        Map<Integer, String> debtCache = Maps.newHashMap();
//        Map<Integer, String> creditCardCache = Maps.newHashMap();
//        String nowDate = DateFormatUtils.yyyyMMdd.format(new Date()) ;
//        if (ListUtils.isNotEmpty(billInfos)) {
//            // 只取最小的日期
//            for (BillInfo billInfo : billInfos) {
//                String billDate = billInfo.getBillDate(); //
//                if (debtCache.get(billInfo.getDebtId()) != null
//                        && Integer.parseInt(debtCache.get(billInfo.getDebtId())) < Integer.parseInt(billDate)) {
//                    continue;
//                }
//                Debt debt = debtService.queryDebt(billInfo.getDebtId());
//                String billInfoId = "2-" + billInfo.getId();
//                debtCache.put(billInfo.getDebtId(), billDate);
//                String billKey = "2-" + debt.getId();
//                billInfoVos.removeIf(infoVo -> infoVo.getKey().equals(billKey));
//                Cust cust = custCache.get(debt.getCustId());
//                if (cust == null) {
//                    cust = custService.queryCust(debt.getCustId());
//                    custCache.put(cust.getId(), cust);
//                }
//                // 插入到 billInfoVos 中
//                BillInfoVo vo = new BillInfoVo();
//                vo.setKey(billKey);
//                vo.setId(billInfoId);
//                vo.setBillDate(billInfo.getBillDate());
//                vo.setBillName(debt.getName());
//                vo.setCustName(cust.getName());
//                vo.setDeadlineDate(billInfo.getDeadlineDate());
//                vo.setTotalAmount(billInfo.getTotalAmount());
//                vo.setMinRepayAmount(billInfo.getTotalAmount());
//                vo.setResidueAmount(billInfo.getResidueAmount());
//                vo.setStatus("未还款");
//                if(Integer.parseInt(nowDate)> Integer.parseInt(vo.getDeadlineDate())) {
//                    vo.setStatus("已逾期");
//                }
//                billInfoVos.add(vo);
//            }
//        }
//        List<Debt> debts = debtService.queryDebtList(new Debt());
//        Set<Integer> debtIds = debtCache.keySet();
//        for (Debt d : debts) {
//            if (debtIds.contains(d.getId())) {
//                continue;
//            }
//            Cust cust = custCache.get(d.getCustId());
//            if (cust == null) {
//                cust = custService.queryCust(d.getCustId());
//                custCache.put(cust.getId(), cust);
//            }
//            BillInfoVo vo = new BillInfoVo();
//            String billInfoId = "2--" + d.getId();
//            vo.setId(billInfoId);
//            vo.setBillDate(d.getNextPayDate());
//            vo.setBillName(d.getName());
//            vo.setCustName(cust.getName());
//            vo.setDeadlineDate("");
//            vo.setTotalAmount(BigDecimal.ZERO);
//            vo.setResidueAmount(BigDecimal.ZERO);
//            vo.setStatus("未出账");
//            billInfoVos.add(vo);
//        }
//        // 再查下信用卡账单
//        CreditCardBill pc = new CreditCardBill();
//        pc.setStatus("R");
//        Map<Integer, Card> cardCache = Maps.newHashMap();
//        List<CreditCardBill> creditCardBills = creditCardBillService.queryCreditCardBillList(pc);
//        if (ListUtils.isNotEmpty(creditCardBills)) {
//            for (CreditCardBill bill : creditCardBills) {
//                String billDate = bill.getBillDate();
//                if (creditCardCache.get(bill.getCardId()) != null
//                        && Integer.parseInt(creditCardCache.get(bill.getCardId())) < Integer.parseInt(billDate)) {
//                    continue;
//                }
//                creditCardCache.put(bill.getCardId(), billDate);
//                Card card = cardCache.get(bill.getCardId());
//                if (card == null) {
//                    card = cardService.queryCard(bill.getCardId());
//                    cardCache.put(card.getId(), card);
//                }
//                Cust cust = custCache.get(card.getCustId());
//                if (cust == null) {
//                    cust = custService.queryCust(card.getCustId());
//                    custCache.put(cust.getId(), cust);
//                }
//                String billKey = "1-" + card.getId();
//                if(ListUtils.isNotEmpty(billInfoVos))
//                    billInfoVos.removeIf(infoVo -> billKey.equals(infoVo.getKey()));
//                BillInfoVo vo = new BillInfoVo();
//                vo.setKey(billKey);
//                String billInfoId = "1-" + bill.getId();
//                vo.setId(billInfoId);
//                vo.setBillDate(bill.getBillDate());
//                String billName = Cache.bankCache.get(card.getBankId()).getName() + "-" + card.getLastFourCode() ;
//                vo.setBillName(billName);
//                vo.setCustName(cust.getName());
//                vo.setDeadlineDate(bill.getDeadlineDate());
//                vo.setTotalAmount(bill.getTotalAmount());
//                vo.setMinRepayAmount(bill.getMinRepayAmount());
//                vo.setResidueAmount(bill.getResidueAmount());
//                vo.setStatus("未还款");
//                if(StringUtils.isNotBlank(vo.getDeadlineDate()) && Integer.parseInt(nowDate) > Integer.parseInt(vo.getDeadlineDate())) {
//                    vo.setStatus("已逾期");
//                }
//                billInfoVos.add(vo);
//            }
//        }
//        CreditCardInfo creditCardInfo = new CreditCardInfo() ;
//        List<CreditCardInfo> creditCardInfos = creditCardInfoService.queryCreditCardInfoList(creditCardInfo) ;
//        Set<Integer> cardIds = cardCache.keySet();
//        for(CreditCardInfo cardInfo:creditCardInfos){
//            if(cardIds.contains(cardInfo.getCardId())) {
//                continue;
//            }
//            BillInfoVo vo = new BillInfoVo();
//            String billInfoId = "1--" + cardInfo.getCardId();
//            vo.setId(billInfoId);
//            vo.setBillDate(cardInfo.getBillDate());
//            Card card = cardCache.get(cardInfo.getCardId());
//            if (card == null) {
//                card = cardService.queryCard(cardInfo.getCardId());
//                cardCache.put(card.getId(), card);
//            }
//            String billName = Cache.bankCache.get(card.getBankId()).getName() + "-" + card.getLastFourCode() ;
//            vo.setBillName(billName);
//            Cust cust = custCache.get(card.getCustId());
//            if (cust == null) {
//                cust = custService.queryCust(card.getCustId());
//                custCache.put(cust.getId(), cust);
//            }
//            vo.setCustName(cust.getName());
//            vo.setDeadlineDate("");
//            vo.setTotalAmount(BigDecimal.ZERO);
//            vo.setResidueAmount(BigDecimal.ZERO);
//            vo.setStatus("未出账");
//            billInfoVos.add(vo);
//        }
//        return billInfoVos;
        return null;
    }


    @RequestMapping(value = "/repayment", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp repayment(@RequestBody IdReq req){
        BaseResp resp = new BaseResp();
        try {
            String occurDate = DateFormatUtils.yyyyMMdd.format(new Date()) ;
            String occurTime = DateFormatUtils.HHmmss.format(new Date()) ;
            String billInfoId = req.getId() ;
            Integer billId = Integer.parseInt(billInfoId.substring(2))  ;
            Integer billType = Integer.parseInt( billInfoId.substring(0,2))  ;
            BillPayRecord billPayRecord = new BillPayRecord();
            billPayRecord.setBillType(billType);
            billPayRecord.setBillId(billId);
            billPayRecord.setOccurDate(occurDate);
            billPayRecord.setOccurTime(occurTime);
            if(billType == 1 ) { // 此为信用卡账单
//                BillInfo bill = billInfoService.queryBillInfo()
//                bill.setStatus("S");
//                billInfoService.modifyDebtBill(bill);

                // 生成 还款记录
//                billPayRecord.setPayAmount(bill.getTotalAmount());
            } else if( billType == 2 ) { // 此为债务账单
//                CreditCardBill bill = creditCardBillService.queryCreditCardBill(billId) ;
//                bill.setStatus("S");
//                billPayRecord.setPayAmount(bill.getTotalAmount());
            }

            billPayRecord.setPayType("1");
            billPayRecord.setStatus("S");

            billPayRecordService.addBillPayRecord(billPayRecord);

        }catch (Exception ex) {
            logger.error("",ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }
}
